<template>
  <div>
    <div
      v-loading="true"
      element-loading-background="var(--ba-bg-color-overlay)"
      element-loading-text="Loading..."
      class="default-main ba-main-loading"
    ></div>
    <div v-if="state.showReload" class="loading-footer">
      <el-button @click="refresh" type="warning">重新登录</el-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import router from "@/router/index";
import { reactive, onMounted, onUnmounted } from "vue";
import { useNavTabs } from "@/store/navTabs";
import { useMemberCenter } from "@/store/memberCenter";
import { isAdminApp } from "@/utils/common";
import { useRoute, useRouter } from "vue-router";
import { getFirstRoute, routePush } from "@/utils/router";
import { userCenterBaseRoute } from "@/router/static"; //引入router
import { Local } from "@/utils/storage";
import { BIGSCREEN } from "@/store/constant/cacheKey";
/**
 * interface Timer extends RefCounted {
                hasRef(): boolean;
                refresh(): this;
                [Symbol.toPrimitive](): number;
            }
 */
let timer: NodeJS.Timer;

const navTabs = useNavTabs();
const memberCenter = useMemberCenter();
const route = useRoute();
const routerS = useRouter();

onMounted(() => {});

//设置最大跳转时间间隔
const state = reactive({
  maximumWait: 1000 * 6,
  showReload: false,
});

//刷新当前页
const refresh = () => {
  router.go(0);
};

if (isAdminApp() && navTabs.state.tabsViewRoutes) {
  //当前是在后台应用内的话获取后台菜单路由列表获取第一个菜单
  let firstPath = getFirstRoute(navTabs.state.tabsViewRoutes);
  console.log(
    "当前是在后台应用内的话获取后台菜单路由列表获取第一个菜单",
    firstPath
  );
  if (firstPath) {
    // 保存第一个菜单路由;
    if (firstPath.path.includes("BigScreen")) {
      memberCenter.keepFirstPath(firstPath.path); //保存至pinia
      Local.set(BIGSCREEN, firstPath.path); //保存至本地
    }
    //跳转当前第一个菜单路由
    routePush(firstPath.path);
  }
} else {
  //当前是在用户中心
  //将用户中心静态路由传递给memberCenter仓库
  const memberCenter = useMemberCenter();
  memberCenter.setViewRoutes(userCenterBaseRoute.children as any);
  // let firstPath = getFirstRoute(memberCenter.state.viewRoutes);
  // if (firstPath) routePush(firstPath.path);
  routePush("/user/account/profile");
}

timer = setTimeout(() => {
  state.showReload = true;
}, state.maximumWait);

onUnmounted(() => {
  clearTimeout(timer);
});
</script>
<style lang="scss" scoped>
.ba-main-loading {
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-footer {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
